﻿using System;
using System.Net.Mail;
using System.Text.RegularExpressions;

namespace OneDollarStore.Helpers
{
    public class EmailHelper
    {
        public static bool Send_Email(string SendFrom, string SendTo, string Subject, string Body)
        {
            //try
            {
                Regex regex = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
                bool result = regex.IsMatch(SendTo);
                if (result == false)
                {
                    // return "Invalid destination email address.";
                    return false;
                }
                else
                {
                    SmtpClient smtp = new SmtpClient();
                    MailMessage msg = new MailMessage(SendFrom, SendTo, Subject, Body);
                    msg.IsBodyHtml = true;
                    smtp.Host = "smtp.gmail.com"; // use SMTP of Gmail
                    smtp.EnableSsl = true;
                    smtp.Send(msg);
                    // return "Your email has been sent to: " + SendTo + ".";
                    return true;
                }
            }
            //catch
            {
                //return false;
            }
        }

        public string Send_Email_With_Attachment(string SendFrom, string SendTo, string Subject, string Body, string AttachmentPath)
        {
            try
            {
                Regex regex = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
                bool result = regex.IsMatch(SendTo);
                if (result == false)
                {
                    return "Invalid destination email address.";
                }
                else
                {
                    try
                    {
                        MailMessage msg = new MailMessage(SendFrom, SendTo, Subject, Body);
                        Attachment attachment = new Attachment(AttachmentPath);
                        msg.Attachments.Add(attachment);
                        msg.Bcc.Add(SendFrom);
                        SmtpClient smtp = new SmtpClient();
                        smtp.Host = "smtp.gmail.com"; // use SMTP of Gmail
                        smtp.Send(msg);
                        return "Your email has been sent to: " + SendTo + ".";;
                    }
                    catch (Exception ex)
                    {
                        return ex.Message;
                    }
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        // SendTo: A list of emails that separated by a semicolon
        public string Send_Email_With_BCC_Attachment(string SendFrom, string SendBCC, string SendTo, string Subject, string Body, string AttachmentPath)
        {
            try
            {
                Regex regex = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
                bool result = true;
                String[] allEmails = SendTo.Split(';');
                foreach (string emailaddress in allEmails)
                {
                    result = regex.IsMatch(emailaddress);
                    if (result == false)
                    {
                        return "Invalid destination email.";
                    }
                }
                if (result == true)
                {
                    try
                    {
                        MailMessage msg = new MailMessage(SendFrom, SendTo, Subject, Body);
                        Attachment attachment = new Attachment(AttachmentPath);
                        msg.Attachments.Add(attachment);
                        msg.Bcc.Add(SendBCC);
                        SmtpClient smtp = new SmtpClient();
                        smtp.Host = "smtp.gmail.com"; // use SMTP of Gmail
                        smtp.Send(msg);
                        return "Your email has been sent to: " + SendTo + ".";;
                    }
                    catch (Exception ex)
                    {
                        return ex.Message;
                    }
                }
                else
                {
                    return "";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
    }
}